package com.le.tester.onjava8.l_2_2_5;

/**
 * createTime：2023/1/7 13:38
 * description：Object.clone()
 */
public class Snake implements Cloneable {

    private Snake next;
    private char c;

    //i==蛇的段数

    public Snake(int i, char x) {
        c = x;
        if (--i > 0) {
            next = new Snake(i, (char) (x + 1));
        }
    }

    //自动递增
    public void increment() {
        c++;
        if (next != null) {
            next.increment();
        }
    }

    //override toString()
    @Override
    public String toString() {
        String s = ":" + c;
        if (next != null)
            s += next.toString();
        return s;
    }

    //override clone
    public Snake clone() {
        try {
            return (Snake) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        Snake snake = new Snake(5, 'a');
        System.out.println("s=" + snake);

        Snake s2 = snake.clone();
        System.out.println("s2=" + s2);

        snake.increment();
        System.out.println("after s.increment,s2=" + s2);

    }

}
